﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using BCCommon.OAEnums.TeamWork;
using BCEntity.OA.TeamWork.TeamWorkAttachment;
using BCCommon;
namespace BCData.OA.TeamWork.TeamWorkAttachment
{
    /// <summary>
    /// 协同办公附件
    /// </summary>
    public class TeamWorkAttachmentData : ITeamWorkAttachmentData
    {
        private const string QUERY_EXISTS = @"SELECT 1 FROM OA_TeamWorkAttachment";
        private const string QUERY = @"SELECT TeamWorkAttachmentId,TeamWorkId,FileSource,FileAddress,CreateTime FROM OA_TeamWorkAttachment";

        private readonly IDatabaseContext databaseContext;

        public TeamWorkAttachmentData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public async Task<bool> Exists(long teamWorkAttachmentId)
        {
            string sql = string.Format(@"{0} WHERE TeamWorkAttachmentId=?TeamWorkAttachmentId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TeamWorkAttachmentId", teamWorkAttachmentId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<TeamWorkAttachmentEntity> Add(TeamWorkAttachmentEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("OA_TeamWorkAttachment", new string[] { "TeamWorkId", "FileSource", "FileAddress" });
            sql = string.Format("{0} {1} WHERE TeamWorkAttachmentId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TeamWorkId", entity.TeamWorkId, DbType.Int64, size: 20);
            parameters.Add("?FileSource", entity.FileSource.ToString(), DbType.AnsiString);
            parameters.Add("?FileAddress", entity.FileAddress?.Trim(), DbType.AnsiString, size: 1000);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<TeamWorkAttachmentEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<TeamWorkAttachmentEntity> Update(TeamWorkAttachmentEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("OA_TeamWorkAttachment", new string[] { "TeamWorkId", "FileSource", "FileAddress" }, new string[] { "TeamWorkAttachmentId" });
            sql = string.Format("{0} {1} WHERE TeamWorkAttachmentId=?TeamWorkAttachmentId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TeamWorkAttachmentId", entity.TeamWorkAttachmentId, DbType.Int64, size: 20);
            parameters.Add("?TeamWorkId", entity.TeamWorkId, DbType.Int64, size: 20);
            parameters.Add("?FileSource", entity.FileSource.ToString(), DbType.AnsiString);
            parameters.Add("?FileAddress", entity.FileAddress?.Trim(), DbType.AnsiString, size: 1000);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<TeamWorkAttachmentEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<bool> Delete(long teamWorkAttachmentId)
        {
            string sql = @"DELETE FROM OA_TeamWorkAttachment WHERE TeamWorkAttachmentId=?TeamWorkAttachmentId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TeamWorkAttachmentId", teamWorkAttachmentId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        public async Task<bool> DeleteBatch(long teamWorkId)
        {
            string sql = @"DELETE FROM OA_TeamWorkAttachment WHERE TeamWorkId=?TeamWorkId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TeamWorkId", teamWorkId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<TeamWorkAttachmentEntity> GetEntity(long teamWorkAttachmentId)
        {
            string sql = string.Format(@"{0} WHERE TeamWorkAttachmentId=?TeamWorkAttachmentId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TeamWorkAttachmentId", teamWorkAttachmentId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<TeamWorkAttachmentEntity>(sql, parameters);
        }

        public async Task<IEnumerable<TeamWorkAttachmentEntity>> GetList(long teamWorkId)
        {
            string sql = string.Format("{0} WHERE TeamWorkId=?TeamWorkId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TeamWorkId", teamWorkId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryAsync<TeamWorkAttachmentEntity>(sql, parameters);
        }

        public async Task<(IEnumerable<TeamWorkAttachmentEntity>, int)> Query(long? teamWorkId, OATeamWorkFileSourceType? fileSourceType, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (teamWorkId.HasValue)
            {
                wheres.Add("TeamWorkId=?TeamWorkId");
                parameters.Add("?TeamWorkId", teamWorkId, DbType.Int64, size: 20);
            }
            if (fileSourceType.HasValue)
            {
                wheres.Add("FileSource=?FileSource");
                parameters.Add("?FileSource", fileSourceType.ToString(), DbType.AnsiString);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY CreateTime DESC LIMIT {2},{3}; SELECT COUNT(1) FROM OA_TeamWorkAttachment {1}",
                   QUERY,
                   wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                   (pageIndex - 1) * pageSize,
                   pageSize);
            return await this.databaseContext.Slave.QueryPageableListAsync<TeamWorkAttachmentEntity>(sql, parameters);
        }
    }
}
